using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ChuanPlus.DAO;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
namespace ChuanPlus.FormControl
{
    public partial class ControlStockCard : DevExpress.XtraEditors.XtraUserControl
    {
        DataTable m_dtStockCard = null;
        public ControlStockCard()
        {
            
            InitializeComponent();
            comboWarehouse.Properties.Items.AddRange(xpColWarehouse);
            dateFrom.DateTime = dateTo.DateTime = DateTime.Today;
        }

        private void buttonGo_Click(object sender, EventArgs e)
        {
            if(m_dtStockCard != null) m_dtStockCard.Clear();
            if (comboWarehouse.SelectedItem != null)
            {
                m_dtStockCard = StockCardDAO.GetWeeklyStockCard(comboWarehouse.SelectedItem as Warehouse, dateFrom.DateTime.Date, dateTo.DateTime.Date);
                grid.DataSource = m_dtStockCard;
            }
            else
                MessageBox.Show("Please select warehouse","Message",MessageBoxButtons.OK,MessageBoxIcon.Information);
            //grid.RefreshDataSource();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            // Checks whether an end-used has double clicked the row indicator.
            GridHitInfo hi = gridView1.CalcHitInfo(grid.PointToClient(MousePosition));
            if (!hi.InRow) return;
            
            // get datasource row by row handle
            int DSRowIndex = gridView1.GetDataSourceRowIndex(hi.RowHandle);
            DataRow row = m_dtStockCard.Rows[DSRowIndex];
            Product product = ProductDAO.GetProduct(Convert.ToInt32(row["ID"]));
            FormViewStockcardDetails frm = new FormViewStockcardDetails(comboWarehouse.SelectedItem as Warehouse, product, dateFrom.DateTime, dateTo.DateTime);
            frm.ShowDialog();
        }

        private void comboWarehouse_Validating(object sender, CancelEventArgs e)
        {
            Warehouse w = comboWarehouse.SelectedItem as Warehouse;
            if (w == null)
            {
                e.Cancel = true;
                comboWarehouse.ErrorText = "warehouse " + comboWarehouse.Text + "does not exsit";
            }
        }
    }
}
